package com.lancoo.lancooknowledgecourseware;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.lancoo.klgcourseware.entity.bean.KlgDbConstant;
import com.lancoo.klgcourseware.manager.KlgCourseWareManager;

import java.util.List;

import com.lancoo.lancooknowledgecourseware.entity.KlgInformationModel;

/**
 * Created by pengmi on 2019/1/3.
 * Description:
 */

public class KlgListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<KlgInformationModel> klgInformationModelList;
    private final Toast toast;


    public KlgListAdapter(Context mContext, List<KlgInformationModel> klgInformationModelList) {
        this.mContext = mContext;
        this.klgInformationModelList = klgInformationModelList;
        toast = Toast.makeText(mContext.getApplicationContext(), "", Toast.LENGTH_LONG);
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_klg, parent, false);
        KlgVH klgVH=new KlgVH(inflate,true);
        return klgVH;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        KlgVH klgVH= (KlgVH) holder;
        klgVH.setKlgName(klgInformationModelList.get(position).getName(),klgInformationModelList.get(position).getNewCode());
    }

    @Override
    public int getItemCount() {
        return klgInformationModelList.size();
    }

    private class KlgVH extends RecyclerView.ViewHolder implements View.OnClickListener{

        protected TextView tv_klg;

        public KlgVH(@NonNull View itemView, boolean isItem) {
            super(itemView);
            if(isItem){
                tv_klg = itemView.findViewById(R.id.tv_klg);
                tv_klg.setOnClickListener(this);
                tv_klg.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        if(toast!=null){
                            toast.setText((String) v.getTag());
                            toast.show();
                        }
                        return false;
                    }
                });
            }

        }

        public void setKlgName(String klgName,String klgCode){
            tv_klg.setText(klgName);
            tv_klg.setTag(klgCode);
        }

        @Override
        public void onClick(View view) {
            String klgCode = (String) view.getTag();
            KlgDbConstant.klg_baseUrl=MainActivity.BASE_URL;
            KlgCourseWareManager.getInstance().launchCourseWareKlg(mContext,klgCode,true);
//            KlgCourseWareManager.getInstance().launchCourseWareKlg(mContext,"CE060420aA00aACAC",true);
//            KlgCourseWareManager.getInstance().launchCourseWareKlg(mContext,"CE060301hA025BCAC",MainActivity.BASE_URL);
//            KlgCourseWareManager.getInstance().launchCourseWareKlg(mContext,"CC040253μA05LBBAA",MainActivity.BASE_URL);
//            KlgCourseWareManager.getInstance().launchCourseWareKlg(mContext,"CE070600BA00BECAA","http://192.168.129.129:10103/Ws_ResourceCloud/");
//            LancooKnowledgeManager.getInstance().startKlgCourseWare(mContext,klgCode);
//            if(PermissionsUtil.getInstance(mContext).requestRecorderPermission(mContext)){
//                if(getAdapterPosition()%2==0){
//
//                }else{
//                    LancooKnowledgeDialogManager.getInstance().openKnowledgeDialog(mContext,klgCode);
//                }
//
//            }
        }
    }

}
